import subprocess
import os
import sys

def run_script(script_name):
    try:
        result = subprocess.run([sys.executable, script_name], check=True, capture_output=True, text=True, encoding='utf-8')
        print(f"Script {script_name} ran successfully.")
        if result.stdout:
            print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"Script {script_name} failed.")
        print(f"Return code: {e.returncode}")
        if e.output:
            print(f"Output: {e.output}")
        if e.stderr:
            print(f"Error: {e.stderr}")
        exit(1)

if __name__ == "__main__":
    base_path = os.path.dirname(os.path.abspath(__file__))
    scripts = [
        'get_klines.py',
        'process_inclusion.py',
        'initial_fractals.py',
        'process_fractals.py',
        'process_final_fractals.py',
        'process_final_ab_fractals.py',
        'validate_adjacent_fractals.py'
    ]

    for script in scripts:
        script_path = os.path.join(base_path, script)
        run_script(script_path)
